package io.bdmc.modules.sys.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;

import io.bdmc.common.utils.HttpContextUtils;
import io.bdmc.common.utils.IPUtils;
import io.bdmc.modules.sys.entity.SysLog;
import io.bdmc.modules.sys.entity.SysUser;

/**
 * Controller公共组件
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年11月9日 下午9:42:26
 */
@CrossOrigin
public abstract class AbstractController {
	protected Logger logger = LoggerFactory.getLogger(getClass());

	protected SysUser getUser() {
		return (SysUser) SecurityUtils.getSubject().getPrincipal();
	}

	protected Long getUserId() {
		return getUser().getUserId();
	}

	protected Long getDeptId() {
		return getUser().getDeptId();
	}

	protected Integer getUserType() {
		return getUser().getUserType();
	}

	public void saveSysLog(String operation, String methodName, String logStr, long time) {
		SysLog sysLog = new SysLog();
		sysLog.setOperation(operation);
		sysLog.setMethod(methodName);
		// 获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		// 设置IP地址
		sysLog.setIp(IPUtils.getIpAddr(request));
		sysLog.setParams(logStr);
		// 用户名
		String username = ((SysUser) SecurityUtils.getSubject().getPrincipal()).getUsername();
		sysLog.setUsername(username);

		sysLog.setTime(time);
		sysLog.setCreateDate(new Date());
		// 保存系统日志
		sysLog.insert();
	}
}
